<!--
<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="关系类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['knowledgeRelationType']"
                :trigger-change="true"
                dictCode="knowledge_relation_type"
                placeholder="请选择关系类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="父节点知识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              &lt;!&ndash;<a-input v-decorator="['knowledgeFromId']" placeholder="请输入父节点知识id"></a-input>&ndash;&gt;
              <a-select
                show-search
                :value="value"
                v-decorator="['knowledgeFromId']"
                placeholder="请输入父节点知识id"
                style="width: 500px"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @search="handleSearch01"
                @change="handleChange01"
              >
                <a-select-option v-for="res in data1" :key="res.value" :value="res.fid">
                  {{ res.text  }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="子节点知识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                show-search
                :value="value1"
                v-decorator="['knowledgeToId']"
                placeholder="请输入子节点知识id"
                style="width: 500px"
                :default-active-first-option="false"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                @search="handleSearch02"
                @change="handleChange02"
              >
                <a-select-option v-for="res2 in data2" :key="res2.value" :value="res2.fid">
                  {{ res2.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JTreeSelect from '@/components/jeecg/JTreeSelect'

import querystring from 'querystring'

import jsonp from 'fetch-jsonp';

let timeout;
let currentValue;
function fetch01(value, callback) {
  console.log(value)
  if (timeout) {
    clearTimeout(timeout);
    timeout = null;
  }
  currentValue = value;
  function fake01() {
    const str = querystring.encode({
      code: 'utf-8',
      q: value,
    });
    console.log("===" + str)
    console.log("ok0")
    jsonp(`http://localhost:8080/jeecg-boot/knowledgeRelation/getFuzzyClues01?${str}`)
      .then(response => {let datee = response.json();console.log(datee) ;return datee})
      .then(res => {
        const data11 = [];
        if (currentValue === value) {
          console.log('res' + JSON.stringify(res))
          res.forEach(r => {
            data11.push({
              value: r.knowledgeTitle,
              text: r.knowledgeTitle,
              fid: r.fid
            })
          })
        }
        callback(data11)
      })
      .catch(err=>{
        console.log(err);
      })

  }
  timeout = setTimeout(fake01, 300);
}
function fetch02(value, callback) {
  console.log(value)
  if (timeout) {
    clearTimeout(timeout);
    timeout = null;
  }
  currentValue = value;
  function fake01() {
    const str = querystring.encode({
      code: 'utf-8',
      q: value,
    });
    console.log("===" + str)
    console.log("ok0")
    jsonp(`http://localhost:8080/jeecg-boot/knowledgeRelation/getFuzzyClues02?${str}`)
      .then(response => {let datee = response.json();console.log(datee) ;return datee})
      .then(res2 => {
        const data11 = [];
        if (currentValue === value) {
          console.log('res' + JSON.stringify(res2))
          res2.forEach(r => {
            data11.push({
              value: r.knowledgeTitle,
              text: r.knowledgeTitle,
              fid: r.fid
            })
          })
        }
        callback(data11)
      })
      .catch(err=>{
        console.log(err);
      })

  }
  timeout = setTimeout(fake01, 300);
}
export default {
  name: 'KnowledgeRelationForm',
  components: {
    JFormContainer,
    JTreeSelect
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: ()=>{},
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      departId:"",
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/knowledgeRelation/add",
        edit: "/knowledgeRelation/edit",
        queryById: "/knowledgeRelation/queryById",
        knowledgeList: "/knowledge/list",
      },
      data1 : [],
      data2 : [],
      value: undefined,
      value1: undefined,
    }
  },
  computed: {
    formDisabled(){
      if(this.formBpm===true){
        if(this.formData.disabled===false){
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton(){
      if(this.formBpm===true){
        if(this.formData.disabled===false){
          return true
        }
      }
      return false
    }

  },
  created () {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
    this.knowledgeList();
  },
  methods: {
    handleSearch01(value) {
      fetch01(value, data => (this.data1 = data));
    },
    handleChange01(value) {
      console.log(value);
      this.value = value;
      fetch01(value, data => (this.data1 = data));
    },
    handleSearch02(value1) {
      fetch02(value1, data1 => (this.data2 = data1));
    },
    handleChange02(value1) {
      console.log(value1);
      this.value1 = value1;
      fetch02(value1, data1 => (this.data2 = data1));
    },
    knowledgeList(){
      let that = this
      getAction(this.url.knowledgeList).then(res=>{
        that.kList = res.result.records
        console.log(that.kList)
      })
    },
    add () {
      this.edit({});
    },
    edit (record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,'knowledgeRelationType','knowledgeFromId','knowledgeToId'))
      })
    },
    //渲染流程表单数据
    showFlowData(){
      if(this.formBpm === true){
        let params = {id:this.formData.dataId};
        getAction(this.url.queryById,params).then((res)=>{
          if(res.success){
            this.edit (res.result);
          }
        });
      }
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据",formData)
          httpAction(httpurl,formData,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row){
      let that = this
      this.$nextTick(()=>{
        console.log(that.form.getField("knowledgeFromId"))
        console.log(that.form.getField("knowledgeToId"))
        that.form.setFieldsValue(pick(row,'knowledgeRelationType','knowledgeFromId','knowledgeToId'))
      })
    },
  }
}
</script>

-->



<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="关系类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['knowledgeRelationType']" :trigger-change="true" dictCode="knowledge_relation_type" placeholder="请选择关系类型"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="父节点知识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!--              <a-input v-decorator="['knowledgeFromId']" placeholder="请输入父节点知识id"></a-input>-->
              <a-select
                show-search
                v-decorator="['knowledgeFromId']"
                placeholder="请输入父节点知识id"
                option-filter-prop="children"
                style="width: 500px"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
              >
                <a-select-option v-for="i in this.kList" :value="i.id">
                  {{i.knowledgeTitle}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="子节点知识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-select
                show-search
                v-decorator="['knowledgeToId']"
                placeholder="请输入子节点知识id"
                option-filter-prop="children"
                style="width: 500px"
                :filter-option="filterOption"
                @focus="handleFocus"
                @blur="handleBlur"
                @change="handleChange"
              >
                <a-select-option v-for="i in this.kList" :value="i.id">
                  {{i.knowledgeTitle}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JTreeSelect from '@/components/jeecg/JTreeSelect'
let knowledgeFromId;

export default {

  name: 'KnowledgeRelationForm',
  components: {
    JFormContainer,
    JTreeSelect
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: ()=>{},
      required: false
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data () {
    return {
      departId:"",
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/knowledgeRelation/add",
        edit: "/knowledgeRelation/edit",
        queryById: "/knowledgeRelation/queryById",
        knowledgeList: "/knowledge/list"
      },
      kList:[],


    }
  },
  computed: {
    formDisabled(){
      if(this.formBpm===true){
        if(this.formData.disabled===false){
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton(){
      if(this.formBpm===true){
        if(this.formData.disabled===false){
          return true
        }
      }
      return false
    }
  },
  created () {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData();
    this.knowledgeList();
  },
  methods: {
    knowledgeList(){
      let that = this
      getAction(this.url.knowledgeList).then(res=>{
        that.kList = res.result.records
        console.log(that.kList)
      })
    },
    add () {
      this.edit({});
    },
    edit (record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model,'knowledgeRelationType','knowledgeFromId','knowledgeToId'))
      })
    },
    //渲染流程表单数据
    showFlowData(){
      if(this.formBpm === true){
        let params = {id:this.formData.dataId};
        getAction(this.url.queryById,params).then((res)=>{
          if(res.success){
            this.edit (res.result);
          }
        });
      }
    },
    submitForm () {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = '';
          let method = '';
          if(!this.model.id){
            httpurl+=this.url.add;
            method = 'post';
          }else{
            httpurl+=this.url.edit;
            method = 'put';
          }
          let formData = Object.assign(this.model, values);
          console.log("表单提交数据",formData)
          httpAction(httpurl,formData,method).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('ok');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }

      })
    },
    popupCallback(row){
      let that = this
      this.$nextTick(()=>{
        console.log(that.form.getField("knowledgeFromId"))
        console.log(that.form.getField("knowledgeToId"))
        that.form.setFieldsValue(pick(row,'knowledgeRelationType','knowledgeFromId','knowledgeToId'))
      })
    },
    handleChange(value) {
      console.log(`selected ${value}`);
      knowledgeFromId = value;
      console.log(value);
      console.log(knowledgeFromId);
    },
    handleBlur() {
      console.log('blur');
      console.log(knowledgeFromId);

    },
    handleFocus() {
      console.log('focus');
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      );
    },
  }
}
</script>